package org.android.cosmus.indiam.tasks;

import org.android.cosmus.indiam.R;
import org.android.cosmus.indiam.activities.MainMenuActivity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.Toast;

public class LoginTask extends AsyncTask<String, Void, Boolean>
{
  private Context mContext;
  private ProgressDialog mDialog;

  public LoginTask(Context context)
  {
    super();
    this.mContext = context;
  }

  @Override
  protected void onPreExecute()
  {
    super.onPreExecute();
    mDialog = new ProgressDialog(mContext);
    mDialog.setMessage("Logging...");
    mDialog.show();
  }
  
  @Override
  protected Boolean doInBackground(String... params)
  {
    String email = params[0];
    String password = params[1];
    /*********************************************************
     * TODO: FAZER O SISTEMA DE LOGIN!!!
     */
    try
    {
      Thread.sleep(2000);
    }
    catch (InterruptedException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return email != null && password != null;
  }

  @Override
  protected void onPostExecute(Boolean isLogged)
  {
    super.onPostExecute(isLogged);
    mDialog.dismiss();
    if (!isLogged)
    {
      String text = mContext.getString(R.string.login_error);
      Toast toast = Toast.makeText(mContext, text, Toast.LENGTH_LONG);
      toast.show();
    }
    else
    {
      mContext.startActivity(new Intent(mContext, MainMenuActivity.class));
    }
  }
}
